#include "ExpressionDrawer.h"
#include "Camera.h"
#include <stdio.h>
#include "Vector3.h"

std::vector<ExpressionMesh*>  m_meshes;
Camera* camera = new Camera(Vector3(0.0, 0.0, -10.0),Vector3(0.0,0.0,0.0), Vector3(0.0,1.0,0.0));
bool mouseLock = false;
int w_height = 0;
int w_width = 0;

ExpressionDrawer::ExpressionDrawer(int *argc, char** argv, int window_width, int window_height) {
	glutInit(argc, argv);
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize (window_width, window_height);
	w_width = window_width;
	w_height = window_height;
	glutInitWindowPosition (100, 100);
	glutCreateWindow (argv[0]);
}

ExpressionDrawer::~ExpressionDrawer() {
	for(unsigned long i=0; i < m_meshes.size(); ++i)
	{
			delete m_meshes.at(i);
	}
}

int ExpressionDrawer::InitOpenGL() {
	//Init

	Variable* phi = new Variable();
	Variable* theta = new Variable();
	Expression* exp = new Sin((Expression*)theta);
	//m_meshes.push_back(new ExpressionMesh(exp, phi, theta, 60, 60));
	//Run scene
	glMatrixMode(GL_MODELVIEW);	//Choisit la matrice MODELVIEW
	glLoadIdentity();	//Réinitialise la matrice
	gluLookAt(0,0,-10,0,0,0,0,1,0);

	//glutMotionFunc(ExpressionDrawer::MouseMotion);
	glutKeyboardFunc(ExpressionDrawer::KeyboardInput);
	glutSpecialFunc(ExpressionDrawer::SpecialKeyboardInput);
	glutDisplayFunc(ExpressionDrawer::DrawGLScene);
	glutReshapeFunc(ExpressionDrawer::ReSizeGLScene);
	glutMainLoop();
	return 0;
}

GLvoid ExpressionDrawer::DrawGLScene() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	//Efface le frame buffer et le Z-buffer

	glBegin(GL_LINES);
		glVertex3d(0.0, 0.0, 0.0);
		glVertex3d(5.0, 0.0, 0.0);
		glVertex3d(0.0, 0.0, 0.0);
		glVertex3d(0.0, 5.0, 0.0);
		glVertex3d(0.0, 0.0, 0.0);
		glVertex3d(0.0, 0.0, 5.0);
	glEnd();

	for(unsigned long i=0; i < m_meshes.size(); ++i)
	{
		//Draw mesh
		m_meshes.at(i)->Draw();
	}
	//camera->Update();
	glutSwapBuffers();
	glutPostRedisplay();
}

GLvoid ExpressionDrawer::ReSizeGLScene(int width, int height) {
	glViewport(0,0,width,height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(70, float(width)/float(height), 0.1, 100);	//Pour les explications, lire le tutorial sur OGL et win
	glMatrixMode(GL_MODELVIEW);
	w_width = width;
	w_height = height;
}

void ExpressionDrawer::AddMesh(ExpressionMesh* mesh) {
	m_meshes.push_back(mesh);
}

GLvoid ExpressionDrawer::SpecialKeyboardInput(int key, int x, int y) {
	switch (key) {
		case GLUT_KEY_LEFT :
			camera->strafeX(true);
			break;
		case GLUT_KEY_RIGHT :
			camera->strafeX(false);
			break;
		case GLUT_KEY_UP :
			camera->strafeZ(true);
			break;
		case GLUT_KEY_DOWN :
			camera->strafeZ(false);
			break;
	}
}

GLvoid ExpressionDrawer::KeyboardInput(unsigned char key, int x, int y) {
	if (key == 27) {
		printf("Escape !!!");
		mouseLock = !mouseLock;
	} else if(key == 122) {
		camera->RotateUp(true);
	} else if(key == 115) {
		camera->RotateUp(false);
	} else if(key == 113) {
		camera->RotateLeft(true);
	} else if(key == 100) {
		camera->RotateLeft(false);
	}
}

GLvoid ExpressionDrawer::MouseMotion(int x, int y) {

};

